" ------------------------
" tab control
" ------------------------
set tabpagemax=10
set showtabline=2
nnoremap <silent><M-w> :tabclose!<Cr>
nnoremap <silent><M-W> :tabonly!<Cr>
nnoremap <silent><Tab>n :tabnext<CR>
nnoremap <silent><Tab>p :tabprevious<CR>
nnoremap <silent><Tab>N :tabm +1<CR>
nnoremap <silent><Tab>P :tabm -1<CR>
nnoremap <Tab>m         :tabm<Space>
" move current buffer to tab
nnoremap <C-w><Cr> <C-w>T
" round current buffer
nnoremap <C-w><Tab> <C-w>r
" open window in tab
nnoremap <leader><Tab> :tabe<Space>
" tab choose
nnoremap <silent><M-1> :tabn1<Cr>
nnoremap <silent><M-2> :tabn2<Cr>
nnoremap <silent><M-3> :tabn3<Cr>
nnoremap <silent><M-4> :tabn4<Cr>
nnoremap <silent><M-5> :tabn5<Cr>
nnoremap <silent><M-6> :tabn6<Cr>
nnoremap <silent><M-7> :tabn7<Cr>
nnoremap <silent><M-8> :tabn8<Cr>
nnoremap <silent><M-9> :tabn9<Cr>
nnoremap <silent><M-0> :tablast<Cr>
" tmux is ignore in gui or windows
if HAS_GUI() || WINDOWS()
    imap <M-o> <C-o>o
    nmap <M-o> o
    imap <M-O> <C-o>O
    nmap <M-O> O
    " TabSwitch
    nnoremap <M-n> gt
    nnoremap <M-p> gT
    nnoremap <silent><M-N> :tabm +1<Cr>
    nnoremap <silent><M-P> :tabm -1<Cr>
    if !has('nvim') && (WINDOWS() || get(g:, 'use_system_browser', 0) == 1)
        let g:browsefilter = ''
        function! s:filter_push(desc, wildcard) abort
            let g:browsefilter .= a:desc . " (" . a:wildcard . ")\t" . a:wildcard . "\n"
        endfunc
        function! s:use_system_browser()
            let l:path = Expand("%:p:h")
            if l:path == '' | let l:path = getcwd() | endif
            if exists('g:browsefilter') && exists('b:browsefilter')
                if g:browsefilter != ''
                    let b:browsefilter = g:browsefilter
                endif
            endif
            exec 'browse tabnew '.fnameescape(l:path)
        endfunc
        call s:filter_push("All Files", "*")
        call s:filter_push("Python", "*.py;*.pyw")
        call s:filter_push("C/C++/Object-C", "*.c;*.cpp;*.cc;*.h;*.hh;*.hpp;*.m;*.mm")
        call s:filter_push("Rust", "*.rs")
        call s:filter_push("Java", "*.java")
        call s:filter_push("Text", "*.txt")
        call s:filter_push("R", "*.r;*.rmd")
        call s:filter_push("Text", "*.txt")
        call s:filter_push("Log", "*.log")
        call s:filter_push("LaTeX", "*.tex")
        call s:filter_push("JavaScript", "*.js;*.vue")
        call s:filter_push("TypeScript", "*.ts")
        call s:filter_push("Php", "*.php")
        call s:filter_push("Vim Script", "*.vim")
        command! UseSystemBrowser call s:use_system_browser()
        nnoremap <silent><M-u> :UseSystemBrowser<Cr>
    endif
endif
" ---------------------------------------
" choose tab using fuzzy_findeer
" ---------------------------------------
if InstalledFzf() && (get(g:, 'prefer_fzf', UNIX()) || !Installed('leaderf'))
    packadd fzf-tabs
    nnoremap <silent><Tab><Tab> :FzfTabs<Cr>
elseif Installed('leaderf')
    packadd leaderf-tabs
    nnoremap <silent><Tab><Tab> :Leaderf tabs<Cr>
endif
" -----------------------------------
" choosewin
" -----------------------------------
PlugAddOpt 'vim-choosewin'
nmap <silent><Tab><Cr> <Plug>(choosewin)
